<?php


namespace backend\modules\business\controllers;


use backend\controllers\AdminController;
use common\models\AdminModel;
use common\models\business\BusinessAdress;
use common\models\business\BusinessBackpig;
use common\models\business\BusinessContract;
use common\models\business\BusinessCustomer;
use common\models\business\BusinessPig;
use common\models\business\BusinessPigPrice;
use common\models\business\BusinessPigPriceDetail;
use common\models\business\BusinessUser;
use common\models\farmerhouse\FarmerhouseHouse;
use common\models\wechat\WechatFans;
use izyue\admin\models\Menu;
use maxwen\easywechat\WechatUser;
use yii;
use common\models\business\BusinessFarmers;
use common\models\business\BusinessPigFile;
use common\models\business\BusinessPerson;
use yii\web\Response;

class SearchController extends AdminController
{

    /**
     * @return array
     * 查找农户
     */
    public function actionSearchFarmer()
    {
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessFarmers::find()
            ->select('name,id,no')
            ->andFilterWhere(['like', 'name', $q])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 查找粉丝openid
     */
    public function actionSearchOpenid()
    {
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = WechatFans::find()
            ->select('nickname,openid as id')
            ->andFilterWhere(['like', 'nickname', $q])
            ->orFilterWhere(['like', 'remark', $q])
            ->limit(50)
            ->asArray()
            ->all();

        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }




    /**
     * @return array
     * 查找用户
     */
    public function actionSearchUser()
    {
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessUser::find()
            ->select('nickname,id')
            ->andFilterWhere(['like', 'nickname', $q])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 查找档案
     */
    public function actionSearchFile()
    {
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessPigFile::find()
            ->select('name,id,no')
            ->andFilterWhere(['like', 'name', $q])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }



    /**
     * @return array
     * 查找仔猪
     */
    public function actionSearchPig(){
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }
        $query = BusinessPig::find()
            ->select('id,no')
            ->andFilterWhere(['like', 'no', $q])
            ->limit(200);

        if($status = Yii::$app->request->get('status')){
            $query->andFilterWhere(['status'=>$status]);
        }

        $data = $query->asArray()->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 查找可以回购的仔猪
     */
    public function actionSearchBackpig(){
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }
        $query = BusinessPig::find()
            ->select('id,no')
            ->andFilterWhere(['like', 'no', $q])
            ->orFilterWhere(['like', 'liveryname', $q])
            ->orFilterWhere(['like', 'headperson', $q])
            ->andFilterWhere(['status'=>BusinessPig::STATUS_AFTERIN])
            ->limit(200);


        $data = $query->asArray()->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 检索可以分割的仔猪
     */
    public function actionSearchPigfg(){
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }
        $query = BusinessPig::find()
            ->select('id,no')
            ->andFilterWhere(['like', 'no', $q])
            ->orFilterWhere(['like', 'liveryname', $q])
            ->orFilterWhere(['like', 'headperson', $q])
            ->andFilterWhere(['status'=>BusinessPig::STATUS_WAITOUT])
            ->andFilterWhere(['not exists',(new yii\db\Query())->select('id')->from(BusinessPigPrice::tableName().' as pprice')->where('pigid='.BusinessPig::tableName().'.id')])
            ->limit(200);

        if($status = Yii::$app->request->get('status')){
            $query->andFilterWhere(['status'=>$status]);
        }

        $data = $query->asArray()->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }



    /**
     * @return array
     * 查找没有巡检员的仔猪
     */
    public function actionSearchNopersonpig(){
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessPig::find()
            ->select('id,no')
            ->andFilterWhere(['like', 'no', $q])
            ->andFilterWhere(['headpersonid'=>0])
            ->limit(200)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 查找没有农户代养的的仔猪
     */
    public function actionSearchNoliverypig(){
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessPig::find()
            ->select('id,no')
            ->andFilterWhere(['like', 'no', $q])
            ->andFilterWhere(['livery'=>0])
            ->limit(200)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 查找业务员
     */
    public function actionSearchPerson(){
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessPerson::find()
            ->select('name,id,no')
            ->andFilterWhere(['like', 'name', $q])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 搜索合同
     */
    public function actionSearchContract(){
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessContract::find()
            ->select('name,id,no')
            ->andFilterWhere(['like', 'name', $q])
            ->orFilterWhere(['like', 'no', $q])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }



    /**
     * @return array
     * 搜索县城
     */
    public function actionSearchCounty(){
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessAdress::find()
            ->select('name,id,lat,lng')
            ->andFilterWhere(['like', 'name', $q])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 搜索乡镇
     */
    public function actionSearchTown(){
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessAdress::find()
            ->select('name,id,lat,lng')
            ->andFilterWhere(['like', 'name', $q])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 搜索村子
     */
    public function actionSearchVillage(){
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessAdress::find()
            ->select('name,id,lat,lng')
            ->andFilterWhere(['like', 'name', $q])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 查找大客户
     */
    public function actionSearchCustomer()
    {
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessCustomer::find()
            ->select('name,id')
            ->andFilterWhere(['like', 'name', $q])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 查找农家乐
     */
    public function actionSearchHouse()
    {
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = FarmerhouseHouse::find()
            ->select('name,id')
            ->andFilterWhere(['like', 'name', $q])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 查找农家乐
     */
    public function actionSearchBackorder()
    {
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessBackpig::find()
            ->select('number as name,id')
            ->andFilterWhere(['like', 'number', $q])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 查找菜单
     */
    public function actionSearchMenu()
    {
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = Menu::find()
            ->select('name,id,route')
            ->andFilterWhere(['like', 'name', $q])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 查找后台用户
     */
    public function actionSearchAdmin()
    {
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = AdminModel::find()
            ->select('username as name,id')
            ->andFilterWhere(['like', 'username', $q])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 查找没有关联购进单的仔猪
     */
    public function actionSearchNopurpig(){
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessPig::find()
            ->select('id,no')
            ->andFilterWhere(['like', 'no', $q])
            ->andFilterWhere(['contract_purid'=>0])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }


    /**
     * @return array
     * 搜索符合重量的库存
     */
    public function actionSearchPigPrice()
    {
        $q = Yii::$app->request->get('q');
        Yii::$app->response->format = Response::FORMAT_JSON;//响应数据格式为json
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = BusinessPigPriceDetail::find()
            ->select('id,weight')
            ->andFilterWhere(['between', 'weight', floatval($q)-10,floatval($q)+10])
            ->andFilterWhere(['status'=>BusinessPigPriceDetail::STATUS_DEFAULT])
            ->limit(50)
            ->asArray()
            ->all();
        $out['results'] = array_map([$this, 'format'],$data);

        return $out;
    }




    private function format($data){
        $result = [];
        $result['id'] = $data['id'];
        $str = '';
        foreach ($data as $key => $row){
            $result[$key] = $row;
            $str .= '['.$row.']';
        }
        $result['text'] = $str;
        return $result;
    }
}